#include <iostream>
struct ListNode {
    int val;          
    ListNode* next;   
    ListNode(int value) : val(value), next(nullptr) {}
};
class LinkedList {
private:
    ListNode* head;  

public:
    LinkedList() : head(nullptr) {}
    void insert(int value) {
        ListNode* newNode = new ListNode(value);

        if (head == nullptr) {
            head = newNode;
        } else {
            ListNode* curr = head;
            while (curr->next != nullptr) {
                curr = curr->next;
            }
            curr->next = newNode;
        }
    }
    void remove(int value) {
        ListNode* curr = head;
        ListNode* prev = nullptr;

        while (curr != nullptr) {
            if (curr->val == value) {
                if (prev == nullptr) {
                    head = curr->next;
                } else {
                    prev->next = curr->next;
                }
                delete curr;
                return;
            }
            prev = curr;
            curr = curr->next;
        }
    }
    ListNode* search(int value) {
        ListNode* curr = head;

        while (curr != nullptr) {
            if (curr->val == value) {
                return curr;
            }
            curr = curr->next;
        }

        return nullptr;
    }
    void printList() {
        ListNode* curr = head;

        while (curr != nullptr) {
            std::cout << curr->val << " -> ";
            curr = curr->next;
        }
        std::cout << "nullptr" << std::endl;
    }
};
int main() {
    LinkedList list;
    list.insert(1);
    list.insert(2);
    list.insert(3);
    list.insert(4);
    std::cout << "Initial List: ";
    list.printList();
    list.remove(3);
    std::cout << "List after removal: ";
    list.printList();
    ListNode* node = list.search(2);
    if (node != nullptr) {
        std::cout << "Node found: " << node->val << std::endl;
    } else {
        std::cout << "Node not found." << std::endl;
    }
    return 0;
}